import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import '@/assets/global.less'
axios.defaults.baseURL='http://big-event-vue-api-t.itheima.net'
Vue.prototype.$http=axios
Vue.use(ElementUI)
Vue.config.productionTip = false

// 定义请求拦截器
axios.interceptors.request.use(function (config) {
  // 为请求头挂载 Authorization 字段
   const {url} = config
  if (url.startsWith('/my')) {
    config.headers.Authorization = store.state.token
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

  let isToken = true
// 定义响应拦截器
axios.interceptors.response.use(function (response) {
  return response
}, function (error) {
  if (error.response.status === 401) {
   if(isToken){
     isToken = false
    Vue.prototype.$message.error('亲,请你重新登录')
    store.commit('updateToken', '')
    router.push('/login')
   }
  }
  return Promise.reject(error)
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
